package com.zego.wawaji.third.zego.im.callback;

import com.zego.wawaji.base.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.third.zego.im.listener.ZegoListener;
import com.zego.zegoliveroom.callback.im.IZegoIMCallback;
import com.zego.zegoliveroom.constants.ZegoIM;
import com.zego.zegoliveroom.entity.ZegoConversationMessage;
import com.zego.zegoliveroom.entity.ZegoRoomMessage;
import com.zego.zegoliveroom.entity.ZegoUserState;

/**
 * 通信回调
 */
public class ZegoIMCallack implements IZegoIMCallback {
    //    private List<CustomZegoUser> allMembers = new ArrayList<>();
    private int audienceNum = 0;   // 初始用户数
    private ZegoListener listener;

    public ZegoIMCallack(ZegoListener listener) {
        this.listener = listener;

//        if (allMembers == null) {
//            allMembers = new ArrayList<>();
//        }
//        allMembers.clear(); // 清理可能遗留上个房间存储用户
    }

    public void setAudienceNum(int audienceNum) {
        this.audienceNum = audienceNum;
    }

    public void release() {
        listener = null;
    }

    /**
     * 房间成员更新回调
     */
    @Override
    public void onUserUpdate(final ZegoUserState[] zegoUserStates, int updateType) {
        MsgMgr.getInstance().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String temp = "";
                for (ZegoUserState state : zegoUserStates) {
                    if (state.updateFlag == ZegoIM.UserUpdateFlag.Added) {
                        //addMember(state);
                        temp = "加入房间";
                        audienceNum++;
                    } else if (state.updateFlag == ZegoIM.UserUpdateFlag.Deleted) {
                        //removeMember(state);
                        temp = "离开房间";
                        audienceNum--;
                    }
                    MsgMgr.getInstance().sendMsg(MsgType.MT_Msg_Receive, state.userName + ": " + temp);
                }
                if (listener != null)
                    listener.roomTotalNum(audienceNum < 0 ? 0 : audienceNum);
            }
        });
    }

    /**
     * 收到房间的广播消息
     */
    @Override
    public void onRecvRoomMessage(String roomId, ZegoRoomMessage[] zegoRoomMessages) {
        for (ZegoRoomMessage roomMessage : zegoRoomMessages) {
            MsgMgr.getInstance().sendMsg(MsgType.MT_Msg_Receive, roomMessage.content);
        }
    }

    /**
     * 收到会话消息
     */
    @Override
    public void onRecvConversationMessage(String s, String s1, ZegoConversationMessage zegoConversationMessage) {

    }

    /**
     * 成员加入
     */
    private void addMember(final ZegoUserState state) {
//        if (allMembers == null) allMembers = new ArrayList<>();
//        boolean found = false;
//        for (CustomZegoUser user : allMembers) {
//            if (TextUtils.equals(user.userID, state.userID)) {
//                found = true;
//                break;
//            }
//        }
//
//        if (!found) {  // 新成员
//            final CustomZegoUser newUser = new CustomZegoUser();
//            newUser.userID = state.userID;
//            newUser.userName = state.userName;
//            allMembers.add(newUser);
//        }
    }

    /**
     * 离开房间
     */
    private void removeMember(final ZegoUserState state) {
//        if (allMembers == null) return;
//
//        CustomZegoUser member = null;
//        for (CustomZegoUser user : allMembers) {
//            if (TextUtils.equals(user.userID, state.userID)) {
//                member = user;
//                break;
//            }
//        }
//
//        if (member != null) {  // 存在改用户并移除用户
//            allMembers.remove(member);
//        }
    }
}
